Scroll Progress Bar

Keywords

Keywords are also called "Reserved words”. There are 32 keywords in 'C'. That is why; 'c' is called as “compact language”.

Keywords in C Programming

auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

auto:

Explanation: The auto keyword is rarely used in modern C as it is the default storage class for local variables. It was used in older versions to specify that a variable has automatic storage duration.

Usage: Declares a local variable with automatic storage duration (the variable's memory is allocated when the block it is defined in is entered and deallocated when the block is exited).

Syntax: auto data_type variable_name;

Example:


void someFunction() {
    auto int x = 10;
    // 'x' has automatic storage duration
}


break:

Explanation: The break keyword is used to exit from the innermost loop or switch statement.

Usage: Used inside loops or switch statements to terminate the loop or switch block prematurely.

Syntax: break;

Example:


for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    printf("Iteration: %d\n", i);
}
// Output: Iteration: 0
//         Iteration: 1
//         Iteration: 2
//         Iteration: 3
//         Iteration: 4

case:

Explanation: The case keyword is used in a switch statement to specify different cases for the value of the controlling expression.

Usage: Used inside a switch block to define different cases to be executed based on the value of the expression.

Syntax: case constant_expression:

Example:


int day = 3;
switch (day) {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("wednesday\n");
        break;
    default:
        printf("Invalid day\n");
}
// Output: wednesday

char:

Explanation: The char keyword is used to declare a variable that can store a single character from the ASCII character set.

Usage: Used to store characters such as letters, digits, and special symbols.

Syntax: char variable_name;

Example:


char grade = 'A';

const:

Explanation: The const keyword is used to declare a constant variable whose value cannot be modified after initialization.

Usage: Used to define constants to prevent accidental modification of their values.

Syntax: const data_type variable_name = value;

Example:


const int MAX_VALUE = 100;

continue:

Explanation: The continue keyword is used to skip the current iteration of a loop and continue with the next iteration.

Usage: Used inside loops to skip specific iterations based on a condition.

Syntax: continue;

Example:


for (int i = 0; i < 5; i++) {
    if (i == 2) {
        continue;
    }
    printf("Iteration: %d\n", i);
}
// Output: Iteration: 0
//         Iteration: 1
//         Iteration: 3
//         Iteration: 4

default:

Explanation: The default keyword is used in a switch statement to specify the default case when no other case matches the value of the controlling expression.

Usage: Used inside a switch block to specify the default behavior if none of the cases match the expression.

Syntax: default:

Example:


int option = 5;
switch (option) {
    case 1:
        printf("Option 1 selected\n");
        break;
    case 2:
        printf("Option 2 selected\n");
        break;
    default:
        printf("Invalid option\n");
}
// Output: Invalid option

do:

Explanation: The do keyword is used in conjunction with while to create a do-while loop, which executes the block of code first and then checks the condition.

Usage: Used to execute a block of code at least once, regardless of the condition.

Syntax: do { ... } while (condition);

Example:


int x = 1;
do {
    printf("Value of x: %d\n", x);
    x++;
} while (x <= 5);
// Output: Value of x: 1
//         Value of x: 2
//         Value of x: 3
//         Value of x: 4
//         Value of x: 5

double:

Explanation: The double keyword is used to declare a variable that can store double-precision floating-point numbers (64-bit) with a higher range and precision compared to float.

Usage: Used to store decimal values with higher precision.

Syntax: double variable_name;

Example:



double pi = 3.1415926535;

else:

Explanation: The else keyword is used in conjunction with if. It specifies an alternative block of code to be executed when the if condition is false.

Usage: Used to define the alternative behavior when the if condition is not satisfied.

Syntax: if (condition) { ... } else { ... }

Example:


int num = -5;
if (num > 0) {
    printf("The number is positive.\n");
} else {
    printf("The number is non-positive.\n");
}
// Output: The number is non-positive.

enum:

Explanation: The enum keyword is used to define an enumerated type, which is a user-defined data type that consists of a set of named constants.

Usage: Used to define a set of related constants with symbolic names.

Syntax: enum enum_name { enumerator1, enumerator2, ... };

Example:


enum Days { SUN, MON, TUE, D, THU, FRI, SAT };
enum Days today = MON;

extern:

Explanation: The extern keyword is used to declare a global variable or function that is defined in another file.

Usage: Used to refer to a global variable or function that is defined in a different source file.

Syntax: extern data_type variable_name; (for variables) extern return_type function_name(arguments); (for functions)

Example:


extern int count; // Declaration of a global variable defined elsewhere
extern void someFunction(); // Declaration of a function defined elsewhere

float:

Explanation: The float keyword is used to declare a variable that can store single-precision floating-point numbers (32-bit) with a moderate range and precision.

Usage: Used to store decimal values with moderate precision.

Syntax: float variable_name;

Example:


float temperature = 25.5;

for:

Explanation: The ‘for’ keyword is used to create a for loop, which allows executing a block of code repeatedly for a specified number of times.

Usage: Used for iterative operations where the number of iterations is known.

Syntax: for (initialization; condition; update) { ... }

Example:


for (int i = 0; i < 5; i++) {
    printf("Iteration: %d\n", i);
}
// Output: Iteration: 0
//         Iteration: 1
//         Iteration: 2
//         Iteration: 3
//         Iteration: 4

goto:

Explanation: The goto keyword is used to transfer control to a specified label in the code.

Usage: Generally discouraged in modern programming due to its potential for creating spaghetti code.

Syntax: goto label_name;

Example:


int num = 5;
if (num == 5) {
    goto found;
}
printf("Number not found.\n");
found:
printf("Number is 5.\n");
// Output: Number is 5.

if:

Explanation: The ‘if’ keyword is used to create a conditional statement, which executes a block of code only if the specified condition is true.

Usage: Used to control the flow of the program based on certain conditions.

Syntax: if (condition) { ... }

Example:


int num = 10;
if (num > 0) {
    printf("The number is positive.\n");
}
// Output: The number is positive.

inline:

Explanation: The inline keyword suggests the compiler to perform inlining for the specified function, which means copying the function's code instead of calling it to improve performance.

Usage: Used to optimize small and frequently used functions for better performance.

Syntax: inline return_type function_name(arguments) { ... }

Example:


inline int square(int x) {
    return x * x;
}

int:

Explanation: The int keyword is used to declare a variable that can store whole numbers (integers).

Usage: Used to store integer values like counts, indices, and mathematical results.

Syntax: int variable_name;

Example:


int age = 25;

long:

Explanation: The long keyword is used to declare a variable that can store large integers with a wider range than int.

Usage: Used to store large integer values that may exceed the range of int.

Syntax: long variable_name;

Example:


long population = 7000000000;

register:

Explanation: The register keyword suggests the compiler to store a variable in a CPU register for faster access. Hover, the compiler may or may not honor this request.

Usage: Rarely used, as modern compilers automatically optimize register allocation.

Syntax: register data_type variable_name;

Example:


register int x = 5;

return:

Explanation: The return keyword is used in a function to return a value to the caller or terminate the function prematurely (for functions with a return type).

Usage: Used to send the result of a function back to the calling code or to exit the function prematurely.

Syntax: return expression;

Example:


int add(int a, int b) {
    return a + b;
}

short:

Explanation: The short keyword is used to declare a variable that can store small integers with a smaller range than int.

Usage: Used to save memory when a small range of integer values is needed.

Syntax: short variable_name;

Example:


short temperature = 30;

signed:

Explanation: The signed keyword is used to declare a variable that can store both positive and negative integer values. It is the default for int.

Usage: Used to specify that a variable can have both positive and negative values.

Syntax: signed data_type variable_name;

Example:


signed int value = -10;

sizeof:

Explanation: The sizeof keyword is a compile-time operator that is used to determine the size in bytes of a data type or variable.

Usage: Used to calculate the size of a data type or variable in bytes.

Syntax: sizeof(data_type); or sizeof(variable_name);

Example:


int size = sizeof(int);

static:

Explanation: The static keyword is used to declare a variable or function with static storage duration, meaning the variable retains its value beten function calls, and the function is only visible within the current source file.

Usage: Used to define variables and functions that preserve their values or visibility throughout the program's execution.

Syntax: static data_type variable_name; (for variables) static return_type function_name(arguments); (for functions)

Example:


static int count = 0; // Static variable with file scope
static void someFunction() { ... } // Static function with file scope

struct:

Explanation: The struct keyword is used to define a user-defined data type that can hold multiple variables of different types under one name.

Usage: Used to create complex data structures by grouping variables together.

Syntax: struct struct_name { data_type member1; data_type member2; ... };

Example:


struct Point {
    int x;
    int y;
};
struct Point p1;

switch:

Explanation: The switch keyword is used to create a switch statement, which allows selecting one of many code blocks to be executed based on the value of a controlling expression.

Usage: Used to choose among several code blocks based on a single value.

Syntax: switch(expression) { case constant1: ... break; case constant2: ... break; ... default: ... }

Example:



int day = 3;
switch (day) {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("wednesday\n");
        break;
    default:
        printf("Invalid day\n");
}
// Output: wednesday

typedef:

Explanation: The typedef keyword is used to create a new name (alias) for an existing data type, which helps in making code more readable and maintainable.

Usage: Used to define aliases for data types, especially for complex data structures.

Syntax: typedef data_type alias_name;

Example:


typedef unsigned long long int ullong;
ullong num = 1000000000000;

union:

Explanation: The union keyword is used to define a user-defined data type that allows storing different data types in the same memory location. The memory allocated is the size of the largest member.

Usage: Used when want to use the same memory location for different types of data.

Syntax: union union_name { data_type member1; data_type member2; ... };

Example:



union Data {
    int x;
    float y;
};
union Data d;

unsigned:

Explanation: The unsigned keyword is used to declare a variable that can store only positive integer values (zero and positive integers). It extends the range of positive integers that can be stored.

Usage: Used when negative values are not needed, to extend the range of positive values.

Syntax: unsigned data_type variable_name;

Example:


unsigned int distance = 500;

void:

Explanation: The void keyword is used to indicate that a function does not return a value or that a pointer does not have a specific data type.

Usage: Used for functions that do not return a value (return type void) or pointers without a specific type (void*).

Syntax: void or void function_name(arguments);

Example:



void showMessage() {
    printf("Hello, world!\n");
}

volatile:

Explanation: The volatile keyword is used to indicate that a variable can be modified by external factors outside the program, such as hardware or other threads.

Usage: Used when a variable can change unexpectedly due to external influences.

Syntax: volatile data_type variable_name;

Example:


volatile int sensorValue; // Used for hardware-related values that can change unexpectedly.

What is the keyword for declaring a variable that won't change its value in C?


const

What keyword is used for creating a loop in C that continues indefinitely until a condition is met?


while

What keyword is used to exit a loop prematurely in C?


break

Which keyword is used to define a new data type in C?


typedef

What keyword is used to include a header file in C?


#include